import java.beans.IntrospectionException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Liu, Yunfeng
 * @DateTime: 2021/7/22 20:57
 * @Description: TODO
 */
public class copyRandomList {
    public Node copyRandomList(Node head) {
        if (head == null) {
            return null;
        }
        Node currentOld = head;
        while (currentOld != null) {
            Node node = new Node(currentOld.val);
            node.next = currentOld.next;
            currentOld.next = node;
            currentOld = node.next;
        }
        currentOld = head;
        Node newhead = head.next;
        Node currentNew = newhead;
        while (currentNew.next != null) {
            if(currentOld.random == null){
                currentNew.random = null;
            }else{
                currentNew.random = currentOld.random.next;
            }
            currentOld = currentNew.next;
            currentNew = currentOld.next;

        }
        if(currentOld.random == null){
            currentNew.random = null;
        }else{
            currentNew.random = currentOld.random.next;
        }
        currentNew = newhead;
        currentOld = head;
        while(currentNew.next != null){
            currentOld.next = currentNew.next;
            currentOld = currentOld.next;
            currentNew.next = currentOld.next;
            currentNew = currentNew.next;
        }
        currentOld.next = null;
        return newhead;
    }
}
